package procilege.impl.eval;

import static java.lang.String.format;

import java.util.logging.Level;
import java.util.logging.Logger;

import procilege.compute.ExprEvaluationTraveller;
import procilege.compute.ExprFormatter;
import procilege.compute.PlaceTypeMetadata;
import procilege.compute.PlaceType;
import procilege.lang.PrivExprElement;
import procilege.lang.PrivExprMergence;
import procilege.lang.PrivilegeForm;
import procilege.lang.PrivilegeFormAnalysis;
import procilege.lang.RelationConformExpr;

import com.google.inject.Inject;

@PlaceTypeMetadata(type = RelationConformExpr.class)
public class RelationConformEvalPlace implements PlaceType<ExprEvaluationTraveller> {

	@Inject
	private PrivilegeFormAnalysis pfa;

	@Inject
	private Logger logger;

	@Inject
	private ExprFormatter ftr;

	public void visit(ExprEvaluationTraveller tr, Object place) {

		RelationConformExpr expr = (RelationConformExpr) place;

		PrivExprElement left = (PrivExprElement) expr.getLeft();
		PrivExprElement right = (PrivExprElement) expr.getRight();

		PrivilegeForm pf1;
		PrivilegeForm pf2;
		PrivExprMergence m1;

		m1 = new PrivExprMergence(null);
		m1.mergePrivilege(left);
		m1.mergePrivilege(right);
		pf1 = pfa.privform(m1);
		pf2 = pfa.privform(right);

		boolean r = pfa.congruent(pf1, pf2);

		if (logger.isLoggable(Level.FINEST)) {
			logger.finest(format("%s *~ %s: %s", ftr.toString(m1), ftr
					.toString(left), (r ? "true" : "false")));
		}
		
		tr.result(r);
	}
}
